package com.ndp.fb;

import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.ndp.fb.config.ConfigCentre;
import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.constant.ConfigConst;
import com.ndp.fb.model.business.bo.nfs.NfsConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by hunk.qin on 2015/4/17.
 */
@SpringBootApplication
@EnableAutoConfiguration
public class ECCommonBeanApplication {

    private Logger logger = LoggerFactory.getLogger(ECCommonBeanApplication.class);
    public JedisPoolConfig redisPool() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(ConfigCentre.getInteger(ConfigConst.REDIS_POOL_MAXTOTAL));
        jedisPoolConfig.setMaxWaitMillis(ConfigCentre.getLong(ConfigConst.REDIS_POOL_MAXWAITMILLIS));
        jedisPoolConfig.setTestOnBorrow(ConfigCentre.getBoolean(ConfigConst.REDIS_POOL_TESTONBORROW));
        jedisPoolConfig.setTestWhileIdle(ConfigCentre.getBoolean(ConfigConst.REDIS_POOL_TESTWHILEIDLE));
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(ConfigCentre.getLong(ConfigConst.REDIS_POOL_TIMEBETWEENEVICTIONRUNSMILLIS));
        jedisPoolConfig.setMinEvictableIdleTimeMillis(ConfigCentre.getLong(ConfigConst.REDIS_POOL_MINEVICTABLEIDLETIMEMILLIS));
        return jedisPoolConfig;
    }

    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig redisPool) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setUsePool(true);
        jedisConnectionFactory.setHostName(ConfigCentre.getString(ConfigConst.REDIS_HOST));
        jedisConnectionFactory.setPort(ConfigCentre.getInteger(ConfigConst.REDIS_PORT));
        jedisConnectionFactory.setDatabase(ConfigCentre.getInteger(ConfigConst.REDIS_DBINDEX));
        jedisConnectionFactory.setPoolConfig(redisPool);
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }
    @Bean(name = "redisTemplate")
    public RedisTemplate redisTemplate() throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setExposeConnection(true);
        redisTemplate.setConnectionFactory(jedisConnectionFactory(redisPool()));
        String keyserialize = ConfigCentre.getString(ConfigConst.REDIS_KEY_SERIALIZE, "org.springframework.data.redis.serializer.StringRedisSerializer");
        String valueserialize = ConfigCentre.getString(ConfigConst.REDIS_VALUE_SERIALIZE, "org.springframework.data.redis.serializer.JdkSerializationRedisSerializer");
        logger.info("KeySerializer Type>>>>>>>>>>>" + keyserialize);
        logger.info("ValueSerializer Type>>>>>>>>>>>" + valueserialize);
        redisTemplate.setKeySerializer((RedisSerializer) Class.forName(keyserialize).newInstance());
        redisTemplate.setValueSerializer((RedisSerializer) Class.forName(valueserialize).newInstance());
        return redisTemplate;
    }
    @Bean
    public NfsConfig nfsConfig() {
        NfsConfig nfsConfig = new NfsConfig();
        nfsConfig.setCharset(CommonConstants.UTF_8);
        nfsConfig.setDomain(ConfigCentre.getString(ConfigConst.NFS_CONFIG_DOMAIN));
        nfsConfig.setPort(ConfigCentre.getString(ConfigConst.NFS_CONFIG_PORT));
        nfsConfig.setProtocol(CommonConstants.HTTP_PROTOCOL);
        nfsConfig.setServerIp(ConfigCentre.getString(ConfigConst.NFS_CONFIG_SERVER_IP));
        nfsConfig.setServerPort(ConfigCentre.getInteger(ConfigConst.NFS_CONFIG_SERVER_PORT));
        return nfsConfig;
    }

    @Bean
    public AmazonS3 s3Client(){
       String accessKeyId = ConfigCentre.getString(ConfigConst.S3_RETENTION_RATE_ACCESS_KEY_ID);
       String secretAccessKey =ConfigCentre.getString(ConfigConst.S3_RETENTION_RATE_SECRET_ACCESS_KEY);
       AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
       AmazonS3 s3 = new AmazonS3Client(credentials);
       Region usWest2 = Region.getRegion(Regions.US_EAST_1);
       s3.setRegion(usWest2);
       return s3;
    }
}

